<?php
namespace Mapi\Controller;
use Think\Controller;
C('SHOW_PAGE_TRACE','True');
class GetuiController extends Controller { 
	public function __construct(){
		parent::__construct();

	}   

   public function index(){
   		/*require_once(I('server.DOCUMENT_ROOT') . '/XgPush/' . 'function.php');

   		dump(PushAllDevices($title='测试推送标题',$content='测试推送内容，测试推送内容，测试推送内容，测试推送内容，',array('jw'=>array('pushCode'=>100,'objectId'=>292,'userId'=>20))));*/

   		$trans=json_encode(array('pushCode'=>100,'objectId'=>$articleId,'userId'=>0));
		sock_post('http://localhost/Mapi/Tui/pushUid.html', array('uid'=>215,'title'=>'见微','content'=>'您的稿件《gsdgdfsgdfg》已经通过审核','trans'=>$trans));			   					
	
   }


























	public function pushMessageToAlias($alias,$clienttype,$title='',$content='',$trans=''){
	    $igt = new \IGeTui(NULL,C('APPKEY'),C('MASTERSECRET'),false);
	    if(strtoupper($clienttype)=='IOS'){
			$template = $this->IGtTransmissionTemplateDemo($title,$content,$trans);
	    }else if(strtoupper($clienttype)=='ANDROID'){
	    	$template = $this->IGtNotificationTemplateDemo($title,$content,$trans);
	    }else{
	    	exit();
	    }   
	    //个推信息体
	    $message = new \IGtSingleMessage();
	    $message->set_isOffline(false);//是否离线
	    $message->set_offlineExpireTime(3600*12*1000);//离线时间
	    $message->set_data($template);//设置推送消息类型
	//	$message->set_PushNetWorkType(0);//设置是否根据WIFI推送消息，1为wifi推送，0为不限制推送
	    //接收方
	    $target = new \IGtTarget();
	    $target->set_appId(C('APPID'));
	    //$target->set_clientId(CID);
	    $target->set_alias($alias);

	    try {
	        $rep = $igt->pushMessageToSingle($message, $target);
	        dump($rep);
	    }catch(RequestException $e){
	        $requstId =e.getRequestId();
	        $rep = $igt->pushMessageToSingle($message, $target,$requstId);
	        dump($rep);
	    }
	}

	public function pushMessageToCid($cid,$clienttype,$title='',$content='',$trans=''){
	    $igt = new \IGeTui(NULL,C('APPKEY'),C('MASTERSECRET'),false);
	    if(strtoupper($clienttype)=='IOS'){
			$template = $this->IGtTransmissionTemplateDemo($title,$content,$trans);
	    }else if(strtoupper($clienttype)=='ANDROID'){
	    	$template = $this->IGtNotificationTemplateDemo($title,$content,$trans);
	    }else{
	    	exit();
	    }
	    //个推信息体
	    $message = new \IGtSingleMessage();
	    $message->set_isOffline(true);//是否离线
	    $message->set_offlineExpireTime(3600*12*1000);//离线时间
	    $message->set_data($template);//设置推送消息类型
	//	$message->set_PushNetWorkType(0);//设置是否根据WIFI推送消息，1为wifi推送，0为不限制推送
	    //接收方
	    $target = new \IGtTarget();
	    $target->set_appId(C('APPID'));
	    $target->set_clientId($cid);
	    //$target->set_alias($alias);

	    try {
	        $rep = $igt->pushMessageToSingle($message, $target);
	        return $rep;
	    }catch(RequestException $e){
	        $requstId =e.getRequestId();
	        $rep = $igt->pushMessageToSingle($message, $target,$requstId);
	        return $rep;
	    }
	}





	public function pushMessageToList($cid_array,$title='',$content='',$trans=''){
	    putenv("gexin_pushList_needDetails=true");
	    putenv("gexin_pushList_needAsync=true");

	    $igt = new \IGeTui(C('HOST'),C('APPKEY'),C('MASTERSECRET'));

	    foreach($cid_array as $clienttype=>$cids){
	    	if(strtoupper($clienttype)=='IOS'){
				$template = $this->IGtTransmissionTemplateDemo($title,$content,$trans);
		    }else if(strtoupper($clienttype)=='ANDROID'){
		    	$template = $this->IGtNotificationTemplateDemo($title,$content,$trans);
		    }else{
		    	continue;
		    }
		    //个推信息体
		    $message = new \IGtListMessage();
		    $message->set_isOffline(true);//是否离线
		    $message->set_offlineExpireTime(3600 * 12 * 1000);//离线时间
		    $message->set_data($template);//设置推送消息类型
		//    $message->set_PushNetWorkType(1);	//设置是否根据WIFI推送消息，1为wifi推送，0为不限制推送
		//    $contentId = $igt->getContentId($message);
		    $contentId = $igt->getContentId($message,"toList任务别名功能");	//根据TaskId设置组名，支持下划线，中文，英文，数字

		    //接收方1
		    $target1 = new \IGtTarget();
			$target1->set_appId(C('APPID'));

		    foreach($cids as $v){			
			    $target1->set_clientId($v);
			    $targetList[$clienttype][] = $target1;
		    }
		    $rep[$clienttype] = $igt->pushMessageToList($contentId, $targetList[$clienttype]);
	    }

	    return $rep;
	}

	/**
	 * 推送给1个uid
	 * @param  int $uid     [description]
	 * @param  string $title   [description]
	 * @param  string $content [description]
	 * @param  string $trans   [description]
	 */
	public function pushMessageToUid($uid,$title='',$content='',$trans=''){
		$cid=M('UserLoginInfo')->where(array('uid'=>$uid))->find();
		//$this->pushMessageToCid($cid,$title,$content,$trans);
		$this->ajaxReturn($this->pushMessageToCid($cid['channelid'],$cid['clienttype'],$title,$content,$trans));
	}

	/**
	 * 推送给多个uid
	 * @param  array $uids     [description]
	 * @param  string $title   [description]
	 * @param  string $content [description]
	 * @param  string $trans   [description]
	 */
	public function pushMessageToUids($uids,$title='',$content='',$trans=''){
		$cid_list=M('UserLoginInfo')->where(array('uid'=>array('in',$uids)))->select();
		foreach($cid_list as $v){
			$cid[$v['clienttype']][]=$v['channelid'];
		}
		dump($cid);
		//$this->pushMessageToList($cid,$title,$content,$trans);
		$this->ajaxReturn($this->pushMessageToList($cid,$title,$content,$trans));
	}

	/**
	 * 推送给一个频道下的所有用户
	 * @param  int $chid     [description]
	 * @param  string $title   [description]
	 * @param  string $content [description]
	 * @param  string $trans   [description]
	 */
	public function pushMessageToChannel($chid,$title='',$content='',$trans=''){
		$uid_list=M('UserInfo')->where(array('userchannel'=>$chid))->select();
		foreach($uid_list as $v){
			$uid[]=$v['uid'];
		}
		//dump($uid);
		$this->pushMessageToUids($uid,$title,$content,$trans);
	}



	
	function IGtTransmissionTemplateDemo($title,$content,$trans){
    $template =  new \IGtTransmissionTemplate();
    $template->set_appId(C('APPID'));//应用appid
    $template->set_appkey(C('APPKEY'));//应用appkey
    $template->set_transmissionType(2);//透传消息类型
    $template->set_transmissionContent($content);//透传内容
    //$template->set_duration(BEGINTIME,ENDTIME); //设置ANDROID客户端在此时间区间内展示消息
    //APN简单推送
       $template = new \IGtAPNTemplate();
       $apn = new \IGtAPNPayload();
       $alertmsg=new \SimpleAlertMsg();
       $alertmsg->alertMsg=$title;
       $apn->alertMsg=$alertmsg;
//        $apn->badge=2;
//        $apn->sound="";
       $apn->add_customMsg("payload",$trans);
       $apn->contentAvailable=1;
       $apn->category="ACTIONABLE";
       $template->set_apnInfo($apn);
       $message = new \IGtSingleMessage();

    //APN高级推送
    /*$apn = new \IGtAPNPayload();
    $alertmsg=new \DictionaryAlertMsg();
    $alertmsg->body="body";
    $alertmsg->actionLocKey="ActionLockey";
    $alertmsg->locKey=$content;
    $alertmsg->locArgs=array("locargs");
    $alertmsg->launchImage="launchimage";
//        IOS8.2 支持
    $alertmsg->title=$title;
    $alertmsg->titleLocKey=$title;
    $alertmsg->titleLocArgs=array("TitleLocArg");

    $apn->alertMsg=$alertmsg;
    $apn->badge=7;
    $apn->sound="";
    $apn->add_customMsg("payload",$trans);
    $apn->contentAvailable=1;
    $apn->category="ACTIONABLE";
    $template->set_apnInfo($apn);*/

    //PushApn老方式传参
//    $template = new IGtAPNTemplate();
//          $template->set_pushInfo("", 10, "", "com.gexin.ios.silence", "", "", "", "");

    return $template;
}


	public function IGtNotificationTemplateDemo($title,$content,$trans){
	    $template =  new \IGtNotificationTemplate();
	    $template->set_appId(C('APPID'));//应用appid
	    $template->set_appkey(C('APPKEY'));//应用appkey
	    $template->set_transmissionType(1);//透传消息类型
	    $template->set_transmissionContent($trans);//透传内容
	    $template->set_title($title);//通知栏标题
	    $template->set_text($content);//通知栏内容
	    $template->set_logo("http://www.appwuhan.net:10085/jianwei.png");//通知栏logo
	    $template->set_isRing(true);//是否响铃
	    $template->set_isVibrate(true);//是否震动
	    $template->set_isClearable(true);//通知栏是否可清除
	    //$template->set_duration(BEGINTIME,ENDTIME); //设置ANDROID客户端在此时间区间内展示消息
	    //iOS推送需要设置的pushInfo字段
	       /*$apn = new \IGtAPNPayload();
	       //$apn->alertMsg = "alertMsg";
	       $apn->badge = 11;
	       $apn->actionLocKey = "启动";
	   //        $apn->category = "ACTIONABLE";
	   //        $apn->contentAvailable = 1;
	       $apn->locKey = $trans;
	       $apn->title = $title;
	       $apn->titleLocArgs = array("titleLocArgs");
	       $apn->titleLocKey = $content;
	       $apn->body = "body";
	       $apn->customMsg = array("payload"=>"payload");
	       $apn->launchImage = "launchImage";
	       $apn->locArgs = array("locArgs");
	
	       $apn->sound=("test1.wav");;
	       $template->set_apnInfo($apn);*/
	    return $template;
	}

	function aliasBind($alias,$cid){
		$igt = new \IGeTui(C('HOST'),C('APPKEY'),C('MASTERSECRET'));
		$rep = $igt->bindAlias(C('APPID'),$alias,$cid);
		$this->ajaxReturn($rep);
	}

	function aliasUnBind($alias,$cid){
		$igt = new \IGeTui(C('HOST'),C('APPKEY'),C('MASTERSECRET'));
       $rep = $igt->unBindAlias(C('APPID'),$alias,$cid);
       $this->ajaxReturn($rep);
	}
}